#include "Sprite.h"
#include "Logger.h"
#include <stdio.h>
#include <stdlib.h>
#include "Shapes2D/BasicRect2D.h"
#include "../OpenGL.h"
#include "Texture.h"

Sprite::Sprite(){
    subimageCount=0;
    subimage=0;
    index=1;
}
Sprite::~Sprite(){
    if (subimage){free(subimage);}
}
void Sprite::resize(){
    if (subimage){subimage=(Img**)realloc(subimage,subimageCount*sizeof(Img));}
    else if(subimageCount==0){free(subimage);}
    else{subimage=(Img**)malloc(subimageCount*sizeof(Img));}
}
Sprite& Sprite::add(Img *newTextureId){
    if (newTextureId==0){errLog.print("Failed to load the image into the sprite");return *this;}//no new image
    subimageCount++;
    resize();
    subimage[subimageCount-1]=newTextureId;
    return *this;
}
Sprite& Sprite::add(const char *fn){
    add(textureDB.make(fn));
    return *this;
}
Sprite& Sprite::extend(Sprite &s){
    int before = subimageCount,i;
    subimageCount+=s.subimages();
    resize();
    for (i=before-1;i<subimageCount;i++)
    {
        subimage[i]=s.tex(i-before+2);
    }
    return *this;
}
Sprite& Sprite::operator+=(Img* newTextureId){
    return add(newTextureId);
}
Sprite& Sprite::operator+=(const char *fn){
    return add(fn);
}
Sprite& Sprite::operator+=(Sprite s){
    return extend(s);
}
Img* Sprite::tex(int pos){
    if (pos<1||pos>subimageCount){return 0;}
    return subimage[pos-1];
}
int Sprite::subimages(){return subimageCount;}
int Sprite::getIndex(){return index;}
Sprite& Sprite::setIndex(int newIndex){
    if (newIndex<1){index=1;}
    else if (newIndex>subimageCount){index=subimageCount;}
    else{index=newIndex;}
    return *this;
}
double Sprite::drawW(Point2D &position, double depth, double widthSize){
    if (subimageCount==0){return 0;}//nothing to draw
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	double heightSize;
	heightSize = widthSize*tex(index)->getWidth()/tex(index)->getHeight();
    index++;
    if (index>=subimageCount){index=1;}
    //mainLog.print(Str()<<"Drawing an image index: "<<index);
    //DRAW STUFF NA0
    glEnable(GL_TEXTURE_2D);
    
	tex(index)->use();//glBindTexture(GL_TEXTURE_2D, m_textureID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	
	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
	
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(position.getX(), position.getY());
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(position.getX() + widthSize, position.getY());
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(position.getX() + widthSize, position.getY() + heightSize);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(position.getX(), position.getY() + heightSize);
	
	glEnd();
    
    glDisable(GL_TEXTURE_2D);
    
    //BasicRect2D(position,widthSize,heightSize).drawPerim(depth+0.1);
    
    return heightSize;
}
double Sprite::drawH(Point2D &position, double depth, double heightSize){
    if (subimageCount==0){return 0;}//nothing to draw
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	double widthSize;
	widthSize = heightSize*tex(index)->getHeight()/tex(index)->getWidth();
    index++;
    if (index>=subimageCount){index=1;}
    //mainLog.print(Str()<<"Drawing an image index: "<<index);
    //DRAW STUFF NA0
    glEnable(GL_TEXTURE_2D);
    
	tex(index)->use();//glBindTexture(GL_TEXTURE_2D, m_textureID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	
	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
	
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(position.getX(), position.getY());
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(position.getX() + widthSize, position.getY());
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(position.getX() + widthSize, position.getY() + heightSize);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(position.getX(), position.getY() + heightSize);
	
	glEnd();
    
    glDisable(GL_TEXTURE_2D);
    
    //BasicRect2D(position,widthSize,heightSize).drawPerim(depth+0.1);
    
    return widthSize;
}
Sprite& Sprite::duplicate(Sprite &spr){
    subimageCount=spr.subimages();
    resize();
    for (int i=1;i<=subimageCount;i++)
    {subimage[i-1]=spr.tex(i);}
    return *this;
}
Sprite& Sprite::operator=(Sprite &spr){
    return duplicate(spr);
}
